#include <stdio.h>
#include <stdlib.h>

// 包含数据结构定义
typedef struct {
    long id;
    char date[11];
    int type;
    char category[20];
    double amount;
    char note[50];
} FinanceRecord;

typedef FinanceRecord DataType;

// 单链表节点
typedef struct Node {
    DataType data;
    struct Node *next;
} SLNode;

// 初始化单链表
void ListInitiate(SLNode **head) {
    *head = (SLNode *)malloc(sizeof(SLNode));
    if (*head == NULL) {
        printf("内存分配失败！\n");
        exit(1);
    }
    (*head)->next = NULL;
}

// 求当前元素个数
int ListLength(SLNode *head) {
    SLNode *p = head;
    int size = 0;
    while(p->next != NULL) {
        p = p->next;
        size++;
    }
    return size;
}

// 插入元素
int ListInsert(SLNode *head, int i, DataType x) {
    SLNode *p, *q;
    int j;
    p = head;
    j = -1;
    
    while(p->next != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    
    if(j != i-1) {
        printf("插入元素位置参数错！\n");
        return 0;
    }
    
    q = (SLNode *)malloc(sizeof(SLNode));
    if (q == NULL) {
        printf("内存分配失败！\n");
        return 0;
    }
    q->data = x;
    q->next = p->next;
    p->next = q;
    return 1;
}

// 删除元素
int ListDelete(SLNode *head, int i, DataType *x) {
    SLNode *p, *s;
    int j;
    p = head;
    j = -1;
    
    while(p->next != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    
    if(j != i-1 || p->next == NULL) {
        printf("删除元素位置参数错！\n");
        return 0;
    }
    
    s = p->next;
    *x = s->data;
    p->next = s->next;
    free(s);
    return 1;
}

// 撤销单链表
void Destroy(SLNode **head) {
    SLNode *p, *p1;
    p = *head;
    while(p != NULL) {
        p1 = p;
        p = p->next;
        free(p1);
    }
    *head = NULL;
}